#!/usr/bin/perl
package csadd;
use strict;
use modules::encode;
sub csadd {
    my ($dbh,$remote,$input,$csnum) = @_;
    my $sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
    $sth->execute;
    my $auth = $sth->fetchrow_array;

    if ($$input[6] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :ADD <channel> <rnick> <level>\n";
    }
    elsif ($$input[6] > 499 || $$input[6] < 50 || $$input[6] !~ /\d+/) {
	print $remote "$csnum O $$input[0] :Bad access range. Use 50-499.\n";
    }
    else {
	$sth=$dbh->prepare("SELECT rnick FROM users WHERE rnick = '$auth' AND level > 449 AND channel = '$$input[4]' AND level > $$input[6];");
	$sth->execute;
	my $true = $sth->fetchrow_array;
	if (!$true) {
	    print $remote "$csnum O $$input[0] :Access denied.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT rnick FROM nicks WHERE rnick = '$$input[5]';");
	    $sth->execute;
	    my $true = $sth->fetchrow_array;
	    if (!$true) {
		print $remote "$csnum O $$input[0] :$$input[5] is not a registered nickname.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT rnick FROM users WHERE rnick = '$$input[5]' and channel = '$$input[4]';");
		$sth->execute;
		my $true = $sth->fetchrow_array;
		if ($true) {
		    print $remote "$csnum O $$input[0] :$$input[5] is already an access holder.\n";
		}
		else {
		    $sth=$dbh->prepare("INSERT INTO users values ('$$input[5]','$$input[4]',$$input[6],0, 0, 0);");
		    $sth->execute;
		    print $remote "$csnum O $$input[0] :Added $$input[5] to \cB$$input[4]\cB\'s access list with level $$input[6]\n";
		}
	    }
	}
    }

}
1;
